﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DBAccessor.Accessor;
using AlanUtilities;
using DBList.Entities;
using DBList;

public partial class CustomerInputDan : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack){
			tbKind.Enabled = false;
			tbName.Enabled = false;
			tbPhone.Enabled = false;
			tbAddress.Enabled = false;
			tbComment.Enabled = false;
		}
	}
	private void RetAllFields()
	{
		tbBarCode.Text = "";
		tbPassword.Text = "";
		tbKind.Text = "";
		tbName.Text = "";
		tbPhone.Text = "";
		tbAddress.Text = "";
		tbComment.Text = "";
		tbBarCode.Enabled = true;
		tbPassword.Enabled = true;
		btnValidate.Enabled = true;

		Label1.Text = "";
		Label2.Text = "";

		tbKind.Enabled = false;
		tbName.Enabled = false;
		tbPhone.Enabled = false;
		tbAddress.Enabled = false;
		tbComment.Enabled = false;
	}
	protected void btnValidate_Click(object sender, EventArgs e)
	{
		Label1.Text = "";
		string strBarCode = tbBarCode.Text.Trim();
		string strpwd = OperateControl.GetMD5String(tbPassword.Text.Trim());
		MsgAccessor msgAcce = new MsgAccessor();
		TiaoMaRecord tmRec = msgAcce.GetOneTiaoMaRecord(strBarCode, null);
		if (tmRec.TiaoMa.ToString() == strBarCode)
		{
			if (!tmRec.IsActivated)
			{
				Label1.Text = @"该礼券还未激活！";
			}
			else if (strpwd == tmRec.PWD)
			{
				if (tmRec.FlowID == 2)
					Label1.Text = @"该礼券已经下过订单！";
				else if (tmRec.FlowID == 3)
					Label1.Text = @"该礼券已经发货出去！";
				else if (tmRec.FlowID == 1)
					Label1.Text = @"该礼券已经提过货！";
				else if (tmRec.FlowID == 0)
				{
					CPNAccessor cpnAcce = new CPNAccessor();
					ClassData kdData = cpnAcce.GetGoodsKindInfo(tmRec.Kind);
					tbKind.Text = kdData.ClassName;

					tbBarCode.Enabled = false;
					tbPassword.Enabled = false;
					btnValidate.Enabled = false;

					tbName.Enabled = true;
					tbPhone.Enabled = true;
					tbAddress.Enabled = true;
					tbComment.Enabled = true;
				}
				else
				{
					Label1.Text = @"该礼券已经失效！";
				}
			}
			else
			{
				Label1.Text = @"券号、密码错误！";
			}
		}
		else
		{
			Label1.Text = @"券号、密码错误！";
		}
	}
	protected void btnSubmit_Click(object sender, EventArgs e)
	{
		if (tbBarCode.Enabled)
		{
			btnValidate_Click(sender, e);
			return;
		}
		Label2.Text = "";
		string strName = tbName.Text.Trim();
		string strPhone = tbPhone.Text.Trim();
		string strAddr = tbAddress.Text.Trim();
		if (strName.Length < 2)
		{
			Label2.Text = @"请输入提货人姓名！";
			return;
		}
		if (strPhone.Length < 7)
		{
			Label2.Text = @"请输入提货人电话！";
			return;
		}
		if (strAddr.Length < 2)
		{
			Label2.Text = @"请输入提货人地址！";
			return;
		}
		string strComment = tbComment.Text.Trim();

		string strBarCode = tbBarCode.Text.Trim();
		string strpwd = OperateControl.GetMD5String(tbPassword.Text.Trim());
		int nUserID = 0;
		MsgAccessor msgAcce = new MsgAccessor();
		string strInfo = strName;
		strInfo += @"/";
		strInfo += strPhone;
		strInfo += @"/";
		strInfo += strAddr;
		int nRet = msgAcce.PickupGoods(0, nUserID, strBarCode, strpwd, strInfo, strComment);
		if (nRet == -100)
		{
			Label2.Text = @"该条码不存在！";
		}
		else if (nRet == -101)
		{
			Label2.Text = @"该礼券还未激活！";
		}
		else if (nRet == -102)
		{
			TiaoMaRecord tmRec = msgAcce.GetOneTiaoMaRecord(strBarCode, null);
			if (tmRec.FlowID == 2)
			{
				Label2.Text = @"该礼券已经下过订单！";
			}
			else if (tmRec.FlowID == 3)
			{
				Label2.Text = @"该礼券已经发货出去！";
			}
			else if (tmRec.FlowID == 1)
			{
				Label2.Text = @"该礼券已经提过货！";
			}
		}
		else if (nRet == 0)
		{
			RetAllFields();
			Label2.Text = "";
			Response.Clear();
			this.ClientScript.RegisterStartupScript(GetType(), @"SUCCESSALERT", @"<Script Language='JavaScript'>window.alert('下单成功！请等待发货！')</script>");
		}
		else
		{
			Label2.Text = @"失败！访问出现异常，请重试！";
		}
	}

	protected void btnReset_Click(object sender, EventArgs e)
	{
		RetAllFields();
	}
}
